package tech.lq0.dreamaticvoyage.gui.menu;

import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuTurbineEngineBlockEntity;
import tech.lq0.dreamaticvoyage.gui.slot.ContainerEnergyData;
import tech.lq0.dreamaticvoyage.gui.slot.SimpleEnergyData;
import tech.lq0.dreamaticvoyage.init.BlockRegistry;
import tech.lq0.dreamaticvoyage.init.MenuTypeRegistry;

public class FukamizuTurbineEngineMenu extends BaseEnergyMenu {

    private final ContainerEnergyData containerData;
    protected final ContainerLevelAccess access;

    public static final int X_OFFSET = 0;
    public static final int Y_OFFSET = 11;

    public FukamizuTurbineEngineMenu(int id, Inventory inventory) {
        this(id, inventory, new SimpleEnergyData(FukamizuTurbineEngineBlockEntity.MAX_DATA_COUNT), ContainerLevelAccess.NULL);
    }

    public FukamizuTurbineEngineMenu(int id, Inventory inventory, ContainerEnergyData containerData, ContainerLevelAccess access) {
        super(MenuTypeRegistry.FUKAMIZU_TURBINE_ENGINE_MENU.get(), id, containerData);

        this.containerData = containerData;
        this.access = access;

        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 9; ++j) {
                this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18 + X_OFFSET, 84 + i * 18 + Y_OFFSET));
            }
        }

        for (int k = 0; k < 9; ++k) {
            this.addSlot(new Slot(inventory, k, 8 + k * 18 + X_OFFSET, 142 + Y_OFFSET));
        }
    }

    public long getPressure() {
        return this.containerData.get(0);
    }

    public long getUCEEnergy() {
        return this.containerData.get(1);
    }

    public long getFEEnergy() {
        return this.containerData.get(2);
    }

    @Override
    public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.slots.get(pIndex);
        if (slot.hasItem()) {
            ItemStack itemstack1 = slot.getItem();
            itemstack = itemstack1.copy();

            if (pIndex < 27) {
                if (!this.moveItemStackTo(itemstack1, 27, 36, true)) {
                    return ItemStack.EMPTY;
                }
            } else {
                if (!this.moveItemStackTo(itemstack1, 0, 27, false)) {
                    return ItemStack.EMPTY;
                }
            }

            if (itemstack1.isEmpty()) {
                slot.setByPlayer(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }
        }

        return itemstack;
    }

    @Override
    public boolean stillValid(Player pPlayer) {
        return this.access.evaluate((level, pos) -> level.getBlockState(pos).is(BlockRegistry.FUKAMIZU_TURBINE_ENGINE.get())
                && pPlayer.distanceToSqr((double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D) <= 64.0D, true);
    }
}
